package com.IOT_LSL;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.LineDataSet;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {
    Fragment Fragment_wifi;
    Fragment Fragment_graph;
    Fragment Fragment_ctl;
    Fragment Fragment_query;
    FragmentManager manager;
    BottomNavigationView bnView;

    public OutputStream tcp_outputStream = null;
    public InputStream  tcp_inputStream  = null;
    public LineDataSet set1 = null;
    public LineDataSet set2 = null;

    public OutputStream get_tcp_outputStream(){
        return tcp_outputStream;
    }
    public void set_tcp_outputStream(OutputStream a){
        this.tcp_outputStream = a;
    }
    public InputStream get_tcp_inputStream(){
        return tcp_inputStream;
    }
    public void set_tcp_inputStream(InputStream a){
        this.tcp_inputStream = a;
    }
    public LineDataSet getSet1(){return set1; }
    public void set_set1( LineDataSet a ){
        this.set1 = a;
    }
    public LineDataSet getSet2(){return set2; }
    public void set_set2( LineDataSet a ){
        this.set2 = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = getSupportFragmentManager();

        bnView = findViewById(R.id.bottom_menu);

        Fragment_ctl = new Fragment_control();
        Fragment_graph = new Fragment_graph();
        Fragment_wifi = new Fragment_wifi();

        //开启一个Fragment事务
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.frame, Fragment_ctl);
        transaction.add(R.id.frame, Fragment_graph);
        transaction.add(R.id.frame, Fragment_wifi);
        transaction.add(R.id.frame, Fragment_query);
        transaction.commit();

        selectFragment(0);

        bnView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.tab_one:
                        selectFragment(0);
                        break;
                    case R.id.tab_two:
                        selectFragment(1);
                        break;
                    case R.id.tab_three:
                        selectFragment(2);
                        break;
                    case R.id.tab_four:
                        selectFragment(3);
                        break;
                }
                return true;
            }
        });
    }

    private void selectFragment(int i) {
        //开启一个Fragment事务
        FragmentTransaction transaction = manager.beginTransaction();
        //隐藏原先的Fragment
        hideFragment(transaction);
        switch (i) {
            case 0:
                transaction.show(Fragment_wifi);
                break;
            case 1:
                transaction.show(Fragment_graph);
                break;
            case 2:
                transaction.show(Fragment_ctl);
                break;
            case 3:
                transaction.show(Fragment_query);
                break;
        }
        //提交事务
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (Fragment_wifi != null) {
            transaction.hide(Fragment_wifi);
        }
        if (Fragment_graph != null) {
            transaction.hide(Fragment_graph);
        }
        if (Fragment_ctl != null) {
            transaction.hide(Fragment_ctl);
        }
        if (Fragment_query != null) {
            transaction.hide(Fragment_query);
        }
    }
}